package cn.silver.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.silver.framework.mybatis.bean.PageModel;
import cn.silver.framework.mybatis.controller.BaseController;
import cn.silver.framework.openapi.model.ResultPage;
import cn.silver.module.system.domain.SysUserRole;
import cn.silver.module.system.bean.UserBean;
import cn.silver.module.system.bean.UserRoleBean;
import cn.silver.module.system.param.UserParam;
import cn.silver.module.system.param.UserRoleParam;
import cn.silver.module.system.service.ISysUserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhouxiaobo
 */
@Validated
@RestController
@RequiredArgsConstructor
@Tag(name = "用户管理-用户授权管理")
@RequestMapping("/system/user/role")
public class SysUserRoleController extends BaseController<ISysUserRoleService, SysUserRole, UserRoleParam, UserRoleBean> {
    @Override
    @Resource
    public void setBaseService(ISysUserRoleService baseService) {
        this.baseService = baseService;
        this.setTitle("用户管理-用户授权管理");
        this.setCode("system_user_role");
        this.setPermission("system:user");
    }

    /**
     * 查询已分配用户角色列表
     */
    @GetMapping("/allocated/{roleId}")
    @SaCheckPermission("system:user:list")
    @Operation(summary = "查询已分配指定角色的用户列表", description = "根据角色ID查询该角色下已分配的用户列表")
    public ResultPage<UserBean> allocated(@PathVariable String roleId, @ModelAttribute UserParam user) {
        PageModel page = this.getPageInfo();
        if (StringUtils.isBlank(page.getOrderByColumn())) {
            page.setOrderByColumn(user.getOrderColumn());
        }
        if (StringUtils.isBlank(page.getIsAsc())) {
            page.setIsAsc(user.getOrderType());
        }
        return baseService.selectAllocatedList(roleId, user, page);
    }

    /**
     * 查询未分配用户角色列表
     */
    @SaCheckPermission("system:user:list")
    @GetMapping("/unallocated/{roleId}")
    public ResultPage<UserBean> unallocated(@PathVariable String roleId, @ModelAttribute UserParam user) {
        PageModel page = this.getPageInfo();
        if (StringUtils.isBlank(page.getOrderByColumn())) {
            page.setOrderByColumn(user.getOrderColumn());
        }
        if (StringUtils.isBlank(page.getIsAsc())) {
            page.setIsAsc(user.getOrderType());
        }
        return baseService.selectUnallocatedList(roleId, user, page);
    }

//    /**
//     * 批量选择用户授权
//     *
//     * @param roleId  角色ID
//     * @param userIds 用户ID串
//     */
//    @SaCheckPermission("system:user:edit")
//    @Log(title = "角色管理", operate = OperateType.GRANT)
//    @PutMapping("/authUser/selectAll")
//    public Result<Void> selectAuthUserAll(String roleId, String[] userIds) {
//        baseService.checkRoleDataScope(roleId);
//        return toAjax(baseService.insertAuthUsers(roleId, userIds));
//    }

//    /**
//     * 取消授权用户
//     */
//    @SaCheckPermission("system:user:edit")
//    @Log(title = "角色管理", operate = OperateType.GRANT)
//    @PutMapping("/authUser/cancel")
//    public Result<Void> cancelAuthUser(@RequestBody SysUserRole userRole) {
//        return toAjax(baseService.deleteAuthUser(userRole));
//    }
//
//    /**
//     * 批量取消授权用户
//     *
//     * @param roleId  角色ID
//     * @param userIds 用户ID串
//     */
//    @SaCheckPermission("system:user:edit")
//    @Log(title = "角色管理", operate = OperateType.GRANT)
//    @PutMapping("/authUser/cancelAll")
//    public Result<Void> cancelAuthUserAll(String roleId, String[] userIds) {
//        return toAjax(baseService.deleteAuthUsers(roleId, userIds));
//    }

}
